package com.cy.pj.sys.service.serviceImpl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.AdminDao;
import com.cy.pj.sys.pojo.Admin;
import com.cy.pj.sys.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;

    @Override
    public PageInfo<Admin> getPageInfo(String keyword, Integer pageNum, Integer pageSize) {
     //1.调用PageHelper的静态方法开启分页功能
        PageHelper.startPage(pageNum,pageSize);
        //2.执行查询
        List<Admin> list = adminDao.selectAdminByKeyword(keyword);
        //3.封装到PageInfo对象中
        return new PageInfo<>(list);
    }

    @Override
    public int deleteAdminById(Integer[] ids) {
        //判断参数合法性
        if(ids==null ||ids.length==0){
            throw new IllegalArgumentException("请选择一个");
        }
        int row = adminDao.deleteAdminById(ids);
        if(row==0){
            throw new ServiceException("记录可能已经不存在");
        }
        return row;

    }

    @Override
    public int insertAdmin(Admin admin) {
        if(admin==null) {
            throw new IllegalArgumentException("保存对象不能为空");
        }
        if(StringUtils.isEmpty(admin.getUserName())) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if(StringUtils.isEmpty(admin.getUserPswd())) {
            throw new IllegalArgumentException("密码不能为空");
        }
       /* if(roleIds==null || roleIds.length==0) {
            throw new IllegalArgumentException("至少要为用户分配角色");
        }*/
        //2.保存用户自身信息
        //2.1对密码进行加密
       /* String userPswd = admin.getUserPswd();
        SimpleHash sh=new SimpleHash(userPswd);*/
        Date date=new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = format.format(date);
        admin.setCreateTime(createTime);
        int row;
        if(admin.getId()==null){
             row= adminDao.insertAdmin(admin);
            return row;
        }else{
             row= adminDao.updateAdminById(admin);
             return row;
        }





    }

  /*  @Override
    public int updateAdminById(Admin admin) {
        if(admin==null) {
            throw new IllegalArgumentException("保存对象不能为空");
        }
        if(StringUtils.isEmpty(admin.getUserName())) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if(StringUtils.isEmpty(admin.getUserPswd())) {
            throw new IllegalArgumentException("密码不能为空");
        }
        Date date=new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = format.format(date);
        admin.setCreateTime(createTime);
        int row ;

        row= adminDao.updateAdminById(admin);
        return row;
    }*/

    @Override
    public Admin findAdminById(Integer id) {
        if(id==null){
            throw new IllegalArgumentException("请先选一个");
        }
        Admin damin = adminDao.findAdminById(id);
        return damin;
    }
}
